/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.util.BundleUtil;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import java.util.List;
import java.util.logging.Logger;

/**
 *
 * @author gdurand
 */
public class FileUploadRenderer extends org.primefaces.component.fileupload.FileUploadRenderer{

    private final String TEMP_ERR_MSG = "URLDecoder: Illegal hex characters in escape (%) pattern";

    @Override
    public void decode(FacesContext context, UIComponent component) {
        try {
            if (context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
                super.decode(context, component);
            }
        } catch (Exception ex) {
            if(ex.getMessage().indexOf(TEMP_ERR_MSG)>-1){
                FacesContext.getCurrentInstance().addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, BundleUtil.getStringFromBundle("dataset.file.uploadWarning"), BundleUtil.getStringFromBundle("dataset.file.upload.include.percent.sign")));
            }else{
                FacesContext.getCurrentInstance().addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, BundleUtil.getStringFromBundle("dataset.file.uploadWarning"), ex.getMessage()));
            }
        }
    }

}